function handles = loadSwitchyard(handles)
% loadSwitchyard
%
% Editing:
% gs may 2005
% --------------------------------------------------

handles.hWaitBar = waitbar(0.25, 'Loading, please wait.');

% check the listboxes to see what and how to load
traceTypeValue = get(handles.lstbxTraceType, 'Value');
traceTypeList = get(handles.lstbxTraceType, 'String');
handles.data.analysis.traceType = traceTypeList{traceTypeValue};
selectionTypeValue = get(handles.lstbxSelectionType, 'Value');
selectionTypeList = get(handles.lstbxSelectionType, 'String');
handles.data.analysis.selectionType = selectionTypeList{selectionTypeValue};

% load
try
    switch handles.data.analysis.traceType
        case 'general physiology traces'
            switch handles.data.analysis.selectionType
                case 'selected traces'
                    mode = 0; 
                    handles = loadTraces(handles, mode);
                otherwise
            end
        case 'excitation profile'
            handles.data.analysis.traceMapYFactor = 5;
            set(handles.traceMapYFactor, 'String', num2str(handles.data.analysis.traceMapYFactor));
            switch handles.data.analysis.selectionType
                case 'selected traces'
                    mode = 0;
                    handles = loadTraces(handles, mode);
                case 'single map'
                    mode = 1;
                    handles = loadTraces(handles, mode);
                otherwise
            end
        case 'input map'
            handles.data.analysis.traceMapYFactor = 100;
            set(handles.traceMapYFactor, 'String', num2str(handles.data.analysis.traceMapYFactor));
            switch handles.data.analysis.selectionType
                case 'selected traces'
                    mode = 0;
                    handles = loadTraces(handles, mode);
                case 'single map'
                    mode = 1;
                    try
                        handles = loadTraces(handles, mode);
                    catch
                        disp(lasterr);
                    end
                case 'multiple maps, select manually'
                    mode = 2;
                    handles = loadTraces(handles, mode);
                case 'multiple maps, from M-file'
                    mode = 3;
                    try
                        handles = loadTraces(handles, mode);
                    catch
                        disp(lasterr);
                    end
                otherwise
            end
        case 'ephys traces'
            handles = loadSetOfTracesNewEphys(handles);
%             data are in handles.data.map.map1.dataArray
            size(handles.data.map.map1.dataArray)
        otherwise
            disp('loadSwitchyard: unrecognized trace type or load method');
    end
    
%     guidata(hObject,handles);

   % filter the traces
    waitbar(0.8, handles.hWaitBar, 'Filtering ...');
    if get(handles.chkFilter,'Value');
        for m = 1 : handles.data.analysis.numberOfMaps
            string = (['handles.data.map.map' num2str(m)]);
            eval(['handles.data.map.mapActive = ' string ';']);
            
            handles.data.map.mapActive.fArray = filterTraceArray(...
                handles.data.map.mapActive.dataArray, handles.data.acq.sampleRate, ...
                handles.data.analysis.popFilterType, handles.data.analysis.filterValue);
            
%             % option to re-filter here:
%             handles.data.map.mapActive.fArray = filterTraceArray(...
%                 handles.data.map.mapActive.fArray, handles.data.acq.sampleRate, ...
%                 'median', 21);
            
            eval([string ' = handles.data.map.mapActive;']);
        end
    end

    % baseline-subtract
    waitbar(0.9, handles.hWaitBar, 'Baseline-subtracting ...');
    if get(handles.chkBaselineSubtract,'Value');
        for m = 1 : handles.data.analysis.numberOfMaps
            string = (['handles.data.map.map' num2str(m)]);
            eval(['handles.data.map.mapActive = ' string ';']);
            
            handles.data.map.mapActive.bsArray = baselineSubtractTraceArrayPhys(...
                handles.data.map.mapActive.fArray, handles.data.acq.sampleRate, ...
                handles.data.analysis.bsBaselineStart, handles.data.analysis.bsBaselineEnd);
            
            eval([string ' = handles.data.map.mapActive;']);
        end
    end
    
    % transfer the first map data set back to the active map directory:
    handles.data.map.mapActive = handles.data.map.map1;
    
    % handles.data.traceNumber = 1;
catch
    disp(lasterr);
end

% select an image
try
    handles = chooseImageFile(handles);
catch
    disp('No ''image'' directory found; no image loaded.');
    handles.data.image.imgDir = [];
    handles.data.image.imgName = [];
    handles.data.image.img = [];
end

close(handles.hWaitBar);
